package com.littleken.servlet;

import com.google.code.kaptcha.Constants;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      //设置编码方式
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        //获取页面输入流
        PrintWriter out = resp.getWriter();

        //获取用户输入的验证码
        String code = req.getParameter("code");
        System.out.println("用户验证码："+ code);
        //获取验证码框架产生的验证码
        String sessionCode = (String) req.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
        System.out.println("生成验证码"+sessionCode);
        //验证
        if(code != null && sessionCode != null ){
            //验证码忽略大小写
            if(code.equalsIgnoreCase(sessionCode)){
                out.print("success");
            }else{
                /*out.print(sessionCode);*/
                out.print("fail");
            }
        }else {
            out.print("fail");
        }
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
